<?php
/**
 * @name /lib/public/StdLib.php
 * @author Michael Foss <webdesign@mikefoss.com>
 * @version 2007.11.28
 * 
 * Standard library.
 */

ini_set('short_open_tag', 'On');

/**
 * Returns the name of the php file;
 * if $saveGET is false, removes the GET vars.
 *
 * @param boolean $saveGET
 * @return string
 */
function _phpSelf($saveGET = true) {
  $s = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'];
  if (!$saveGET) {
    // Split the string through the ? delimiter
    $tok = strtok($s, '?');
    // Take the first half, the location
    $s = $tok;
  }
  return $s;
}

/**
 * Prints the name of the php file;
 * if $saveGET is false, removes the GET vars.
 *
 * @param boolean $saveGET
 */
function phpSelf($saveGET = true) {
  $str = _phpSelf($saveGET);
  print($str);
}

/**
 * Returns the webroot; if $loc is used, appends it.
 *
 * @param string $loc
 * @return string
 */
function _webroot($loc = "") {
  if (!$loc || $loc[0] == "/" || $loc[0] == "\\")
    $loc = $GLOBALS["WEBROOT"] . $loc;
  return $loc;
}

/**
 * Prints the webroot; if $loc is used, appends it.
 *
 * @param string $loc
 */
function webroot($loc = "") {
  $str = _webroot($loc);
  print($str);
}

/**
 * Returns the webroot via https; if $loc is used, appends it.
 *
 * @param string $loc
 * @return string
 */
function _swebroot($loc = "") {
  if (!$GLOBALS['ALLOW_HTTPS'])
    $loc = _webroot($loc);
  else {
    if (!$loc || $loc[0] == "/" || $loc[0] == "\\")  // Add a prefix
      $loc = $GLOBALS["WEBROOT"] . $loc;
    if (substr($loc, 0, 7) == "http://")  // Only replace the prefix
      $loc = substr_replace($loc, "https://", 0, 7);
  }
  return $loc;
}

/**
 * prints the webroot via https; if $loc is used, appends it.
 *
 * @param string $loc
 */
function swebroot($loc = "") {
  $str = _swebroot($loc);
  print($str);
}

/**
 * Returns the graphics root; if $loc is used, appends it.
 *
 * @param string $loc
 * @return string
 */
function _gfxroot($loc = "") {
  global $Config;
  if (!$loc || $loc[0] == "/" || $loc[0] == "\\")
    $loc = $GLOBALS["WEBROOT"] . '/graphics' . $loc;
  if (IsSecurePage() && $Config->CanBeSecure)
    $loc = 'https://' . substr($loc, 7);
  return $loc;
}

/**
 * Prints the graphics root; if $loc is used, appends it.
 *
 * @param string $loc
 */
function gfxroot($loc = "") {
  $str = _gfxroot($loc);
  print($str);
}

/**
 * Prints the string plus a \n character.
 *
 * @param string $s
 */
function println($s = "") {
  print("$s\n");
}

/**
 * Returns the MyEnv session vars if no other set is specified in $key;
 * if it is, returns the session vars in MyEnv[$key].
 *
 * @param string $key
 * @return Mixed[]
 */
function & GetSessionVars($key = false) {
  global $Config;
  if ($key === false)
    return $_SESSION[$Config->SiteName]['MyEnv'];
  else
    return $_SESSION[$Config->SiteName][$key];
}

/**
 * Sets whether or not https is being used.
 *
 * @param boolean $IsSecure
 */
function SetSecurePage($IsSecure = true) {
  $_SERVER['HTTPS'] = $IsSecure;
}

/**
 * Returns true if current page is marked as secure, false otherwise.
 *
 * @return boolean
 */
function IsSecurePage() {
  return $_SERVER['HTTPS'] == 'on';
}